/*
 * 文件名：LoginAuthReqHandler.java
 * 版权：亚信科技(南京)版权所有
 * 描述：TODO
 * 修改人：xuwei3
 * 修改时间：2015-11-10
 * 修改内容：TODO
 */
package com.ailk.bigdata.etl.realstream.server.protocal.sdtp.netty.handler;

import java.nio.charset.Charset;
import java.util.Random;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

import com.ailk.bigdata.etl.realstream.server.model.Header;
import com.ailk.bigdata.etl.realstream.server.model.MessageType;
import com.ailk.bigdata.etl.realstream.server.model.SdtpDataPack;
import com.google.common.primitives.UnsignedInteger;

/**
 * @description TODO
 * @author [xuwei3]
 * @version [版本号,2015-11-10]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
public class LoginAuthReqHandler extends ChannelInboundHandlerAdapter
{
	private static Logger logger = LoggerFactory.getLogger(LoginAuthReqHandler.class);
	private static Charset utf8Charset = Charset.forName("utf-8");

	/**
	 * @see io.netty.channel.ChannelInboundHandlerAdapter#channelActive(io.netty.channel.ChannelHandlerContext)
	 */
	@Override
	public void channelActive(ChannelHandlerContext ctx) throws Exception
	{
		ctx.writeAndFlush(buildLoginReq());
	}

	/**
	 * @see io.netty.channel.ChannelInboundHandlerAdapter#channelRead(io.netty.channel.ChannelHandlerContext,
	 *      java.lang.Object)
	 */
	@Override
	public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception
	{
		SdtpDataPack dataPack = (SdtpDataPack) msg;
		if (null != dataPack.getHeader() && dataPack.getHeader().getMessageType() == MessageType.linkAuth_Resp.getValue())
		{
			ByteBuf dataContent = dataPack.getDataContent();

			System.out.println(dataContent.readUnsignedByte());
			System.out.println(new String(dataContent.readBytes(64).array(), utf8Charset).trim());
			logger.info("=========================");
		}
		else
		{
			ctx.fireChannelRead(dataPack);
		}
	}

	private SdtpDataPack buildLoginReq()
	{
		SdtpDataPack loginDataPack = new SdtpDataPack();
		Header header = new Header();
		header.setMessageType(MessageType.linkAuth_Req.getValue());
		header.setSequenceId(UnsignedInteger.valueOf(4294967295L));
		header.setTotalContents((short) 1);
		header.setTotalLength(91);
		loginDataPack.setHeader(header);

		ByteBuf byteBuf = Unpooled.buffer(82);
		byteBuf.writeBytes("beijing_ocdp".getBytes());
		byteBuf.writeBytes("9294ed8e999b895180fd8210fec00a52a123fdd5522cb5ee7738632a425c7e23".getBytes());
		byteBuf.writeLong(System.currentTimeMillis() / 1000);
		byteBuf.writeShort(new Random().nextInt(2));
		loginDataPack.setDataContent(byteBuf);

		System.out.println(header.getMessageType());
		System.out.println(header.getSequenceId());
		return loginDataPack;
	}

	public static void main(String[] args)
	{
		System.out.println("qazwsxedcrfv".getBytes().length);
		System.out.println(System.currentTimeMillis() / 1000);
		System.out.println(UnsignedInteger.MAX_VALUE);
	}
}
